//
//  AddressViewController.swift
//  ShopIOS
//
//  Created by Apple on 2019/7/19.
//  Copyright © 2019 Apple. All rights reserved.
//

import UIKit

class AddressViewController: UIViewController,UITableViewDataSource,UITableViewDelegate {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return ShippingAddressList==nil ? 0 :ShippingAddressList.ShippingAddress.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell=tableview.dequeueReusableCell(withIdentifier: "re") as! AddressTableViewCell
        let address=ShippingAddressList.ShippingAddress[indexPath.row]
        if address.IsDefault{
            cell.lb_default.isHidden=false
        }else{
            cell.lb_default.isHidden=true
        }
        cell.lb_default.textColor=UIColor.red
        cell.lb_default.backgroundColor=UIColor.orange
        cell.lb_default.layer.cornerRadius=5
        cell.lb_default.clipsToBounds=true
        cell.lb_name.text=address.ShipTo
        cell.lb_phone.text=address.Phone
        cell.lb_address.text="\(address.RegionFullName!)\(address.Address!)"
        cell.lb_first.text=address.ShipTo?.prefix(1).description
        cell.lb_first.layer.cornerRadius=15
        
        cell.lb_edit.isUserInteractionEnabled = true
        cell.lb_delete.isUserInteractionEnabled = true
        let signTap1:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(edit(sender:)))
        let signTap2:UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(edit(sender:)))
        cell.lb_edit.addGestureRecognizer(signTap1)
        cell.lb_delete.addGestureRecognizer(signTap2)
        signTap1.view!.tag = indexPath.row
        signTap2.view!.tag = indexPath.row
        cell.selectionStyle = .none
        return cell
    }
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if choose {
            NotificationCenter.default.post(name: NSNotification.Name(rawValue: "choose"), object: ShippingAddressList.ShippingAddress[indexPath.row])
            self.navigationController?.popViewController(animated: true)
        }
    }
    

    @IBOutlet weak var uv_back: UIView!
    @IBOutlet weak var lb_addnew: UILabel!
    @IBOutlet weak var tableview: UITableView!
    var ShippingAddressList:mShippingAddressList!
    var choose:Bool=false
    init(choose:Bool){
        super.init(nibName: nil, bundle: nil)
        self.choose=choose
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        uv_back.addOnClickListener(target: self, action: #selector(back))
        lb_addnew.addOnClickListener(target: self, action: #selector(addnew))
        tableview.separatorStyle = .none
        tableview.register(UINib(nibName: "AddressTableViewCell", bundle: nil), forCellReuseIdentifier: "re")
        loaddata()
        NotificationCenter.default.addObserver(self, selector: #selector(update), name: NSNotification.Name("updateAddress"), object: nil)
        // Do any additional setup after loading the view.
    }
    @objc func update(){
        loaddata()
    }
    @objc func edit(sender:UITapGestureRecognizer){
        let index=sender.view!.tag
        let address=ShippingAddressList.ShippingAddress[index]
        self.navigationController?.pushViewController(AddNewAddrViewController(shippingAddress: address), animated: true)
    }
    @objc func addnew(){
        self.navigationController?.pushViewController(AddNewAddrViewController(), animated: true)
    }
    @objc func back(){
        self.navigationController?.popViewController(animated: true)
    }
    func loaddata(){
        var d = ["userkey":userkey!,
                 "app_key":app_key,
                 "timestamp":getTimestamp()
            ] as [String : String]
        let sign=SignTopRequest(params: d)
        d["sign"]=sign
        AlamofireHelper.get(url: GetShippingAddressList, parameters: d, successHandler: {[weak self](res)in
            HUD.dismiss()
            guard let ss = self else {return}
            ss.ShippingAddressList=mShippingAddressList.init(json: res)
            ss.tableview.reloadData()
        }){[weak self] (error) in
            HUD.dismiss()
            guard let ss = self else {return}
        }
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
